//
//  HERecoverViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/25.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HERecoverViewController.h"
#import "HEOrgModel.h"
#import "HENurseOrgCell.h"
#import "HERealNameAuthAlterController.h"
#import "HEHealthArchiveModel.h"
#import "HESpinalActiveViewController.h"
#import "HEStoreGoodListViewController.h"
#import "HEMiniTools.h"

@interface HERecoverViewController ()<UITableViewDelegate, UITableViewDataSource>


@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray <HEOrgModel *> * dataSourceArr;

@end

@implementation HERecoverViewController

- (NSMutableArray<HEOrgModel *> *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"康复上门";
    [self initUI];
    [self loadData];
    
    [self setCustomTableView:_tableView];
}

- (void)initUI {
    
    _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, KScreenHeight - ZXNavBarHeight) style:UITableViewStylePlain];
//    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.separatorColor = KHexColor(@"EEEEEE");

    _tableView.scrollsToTop = YES;
    _tableView.tableFooterView = [[UIView alloc] init];
    
    self.tableView.backgroundColor = KWhiteColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    __weak __typeof(&*self)weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        weakSelf.isFromHeadRefresh = YES;
        [weakSelf loadData];
    }];
 
 
    [_tableView registerClass:[HENurseOrgCell class] forCellReuseIdentifier: KHENurseOrgCell];
}

- (void)loadData {
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KOrgServer parameters:@{@"server_type": @2} success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);
        
        if (self.isFromHeadRefresh) {
            [self.dataSourceArr removeAllObjects];
        }
        
        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];
        [self.dataSourceArr addObjectsFromArray:[HEOrgModel mj_objectArrayWithKeyValuesArray:response.data]];
        [self.tableView reloadData];
        
        [self.tableView.mj_header endRefreshing];
 
        if (self.dataSourceArr.count == 0) {
            [self showEmpty];
        }

    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
     }];
}



#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HENurseOrgCell *cell = [tableView dequeueReusableCellWithIdentifier: KHENurseOrgCell];
    cell.model = _dataSourceArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier: KHENurseOrgCell cacheByIndexPath:indexPath configuration:^(HENurseOrgCell *cell) {
        cell.model = self.dataSourceArr[indexPath.row];
    }];
}

- (void)loadverifyInfoData:(HEOrgModel *)orgModel {
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KgetPhrUserInfo parameters:@{} success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);
        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
        if (response.success) {
            HEHealthArchiveModel *model = [HEHealthArchiveModel mj_objectWithKeyValues:response.data];
            [self toWxMini: model org:orgModel];
        }

    } failure:^(NSError * _Nonnull error) {
     }];
}

- (void)toWxMini:(HEHealthArchiveModel *)model org:(HEOrgModel *)orgModel{
    
    UserInfo *user = [[UserManager sharedUserManager] getUserInfo];

    NSDictionary *dict = @{
        @"cardNum" : user.healthCode,
        @"address" : model.hjdz,
        @"phone" : user.username,
        @"idCardNo" : model.sfzhm,
        @"sex" : [@"男" isEqualToString:model.xbmc] ? @1 : @2,
        @"userName" : model.xm,
        @"age" : model.csrq
    };

    NSString *encodeData = [[dict mj_JSONString] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    encodeData = [[dict mj_JSONString] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSString *path = [NSString stringWithFormat:@"%@%@%@&id=%@&name=%@", orgModel.org_callurl, @"?source=app&query=", encodeData, orgModel.Kparams[@"id"], orgModel.Kparams[@"name"]];
    [HEMiniTools jumpToWximin:path username:orgModel.org_init_appid];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HEOrgModel *model = _dataSourceArr[indexPath.row];
    HEStoreGoodListViewController *vc = [HEStoreGoodListViewController new];
    vc.storeId = [((NSNumber *)model.Kparams[@"store_id"]) integerValue];
    [self.navigationController pushViewController:vc animated:YES];
}
@end
